package customerService

import (
	"fmt"
	model "go_code/project01_basic/chapter10_projectDemo/project02_customerManagerSystem/customerModel"
)

type CustomerService struct {
	CustomerSlice []*model.Customer
	id            int
	Flag          bool
}

func (c *CustomerService) AddCustomer() {
	var c1 *model.Customer
	var name, gender, phone, email string
	var age int
	fmt.Println("请依次输入姓名 性别 年龄 电话 邮箱:")
	n, err := fmt.Scanln(&name, &gender, &age, &phone, &email)
	fmt.Println(n, err)
	c.id++
	c1 = c1.NewCustomer(c.id, name, gender, age, phone, email)
	c.CustomerSlice = append(c.CustomerSlice, c1)
	fmt.Printf("添加客户成功,添加的客户为:\n")
	c1.GetInfo()
	fmt.Println()
}

func (c *CustomerService) ChangeCustomer() {
	var id int
	fmt.Println("请输入需要修改的客户id:")
	fmt.Scanln(&id)
	if id > c.id || id < 1 {
		fmt.Println("id不正确")
		return
	}
	fmt.Println("你需要修改的客户为:")
	c.CustomerSlice[id-1].GetInfo()
	fmt.Println("请按提示输入修改过的客户信息:")
	var name, gender, phone, email string
	var age int
	fmt.Println("请依次输入姓名 性别 年龄 电话 邮箱:")
	n, err := fmt.Scanln(&name, &gender, &age, &phone, &email)
	fmt.Println(n, err)
	c.CustomerSlice[id-1].ChangeCustomer(name, gender, age, phone, email)
	fmt.Println("客户修改成功,更新之后的客户信息为:")
	c.CustomerSlice[id-1].GetInfo()
}

func (c *CustomerService) CustomerList() {
	fmt.Println("----------客户列表----------")
	fmt.Printf("编号\t名称\t性别\t年龄\t电话\t邮箱\n")
	if len(c.CustomerSlice) == 0 {
		fmt.Printf("暂时没有客户,可以在主菜单添加客户\n\n")
	} else {
		for _, item := range c.CustomerSlice {
			item.GetInfo()
		}
		fmt.Println()
	}
}

func (c *CustomerService) DeleteCustomer() {
	var id int
	fmt.Println("请输入需要删除的客户id:")
	fmt.Scanln(&id)
	if id > c.id || id < 1 {
		fmt.Println("id不正确")
		return
	}
	fmt.Println("你需要删除的客户为:")
	for i, v := range c.CustomerSlice {
		if v.GetId() == id {
			c.CustomerSlice[i].GetInfo()
			c.CustomerSlice = append(c.CustomerSlice[:i], c.CustomerSlice[i+1:]...)
			fmt.Println("客户删除成功")
		}
	}
}

func (c *CustomerService) Exit() {
	var str string
	fmt.Println("确定要退出吗")
	fmt.Println("y/n")
	fmt.Scanln(&str)
	if str == "y" {
		c.Flag = true
		fmt.Println("Exit successfully")
	}
}
